var myScroll,
    pullDownEl, pullDownOffset,
    pullUpEl, pullUpOffset;

$(function(){
	//初始化下拉和上拉箭头
	pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');  
    pullUpOffset = pullUpEl.offsetHeight;
	
    //拖拽刷新
	myScroll = new iScroll('wrapper',{
		scrollbarClass: 'myScrollbar',
		onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
                this.minScrollY = 0;
                $("#pullDown").show();
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                this.minScrollY = -pullDownOffset;
                $("#pullDown").show();
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                this.maxScrollY = this.maxScrollY;
                $("#pullUp").show();
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                this.maxScrollY = pullUpOffset;
                $("#pullUp").show();
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';               
                pullDownAction();   // ajax call
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';               
                pullUpAction(); // ajax call
            }
        }
	});
	
	//向上拖拽刷新
	function pullUpAction(){
		var status = $("#status").val();
		OrderApp.loadData(OrderApp.getPage(),status);
	}
	
	//向下拖拽刷新
	function pullDownAction(){
		var status = $("#status").val();
		OrderApp.loadData(OrderApp.getPage(),status);
	}
	
	//初始化首页订单
	OrderApp.init();
	
	//点击切换头部订单状态
	$("nav.order-nav ul.clear li a").click(function(){
		
		$("nav.order-nav ul.clear li a").removeClass('order_select');
		$(this).addClass('order_select');
		var name = $(this).attr('name');
		OrderApp.setPage(1);
		OrderApp.loadData(1,name);
	});
});
var OrderApp = function(){
	var page = 1;
	var pageSize = 6;
	return{
		/**
		 * 初始化
		 */
		init:function(){
			var status = $("#status").val();
			OrderApp.loadData(1,status);
		},
		/**
		 * 设置当前页数
		 */
		setPage:function(pg){
			OrderApp.page = pg;
		},
		/**
		 * 获取当前页面
		 */
		getPage:function(){
			
			return page;
		},
		/**
		 * 加载数据
		 */
		loadData:function(p,status){
			if(!!!status)status == "";
			
			var url = $("#APP_ROOT_PATH").val() + 'order/page_data';
			var params = {page:p,pageSize:pageSize,status:status};
			mui.util.Ajax.asyncGET(url,params,function(res){
				layer.closeAll();
				
				var loaded = (p - 1) * pageSize + res.rows.length;
				if(res.rows.length <= 0 || loaded <= res.total){
					$(".bottom-1").text('没有更多订单啦');
				}else{
					$(".bottom-1").text('');
				}
				
				var data = {data:res.rows};
				var bt=baidu.template;
				var html=bt('t:bt',data);
				if(p == 1){
					$("#data-content").html(html);
				}else{
					$("#data-content").append(html);
				}
				myScroll.refresh();
				$("#pullDown, #pullUp").hide();
				
				page++;
			},function(){
				layer.closeAll();
			},function(){
				layer.open({type: 2});
			});
		},
		/**
		 * 新增订单
		 */
		create:function(){
			var url = $("#APP_ROOT_PATH").val() + 'order/add';
			var array = new Array();
			var data = {id:1,num:1};
			array.push(data);
			
			mui.util.Ajax.asyncGET(url,{json:JSON.stringify(array),aId:1},function(res){
				if(res.ret == 0){
					alert(res.flag);
				}else{
					alert(res.message);
				}
			});
		},
		/**
		 * 新增砍价活动订单
		 */
		createDisCount:function(){
			var url = $("#APP_ROOT_PATH").val() + 'order/add_discount';
			var id = $("#id").val();
			var aId = $("#addressId").val();
			mui.util.Ajax.asyncGET(url,{discountId:id,aId:aId},function(res){
				if(res.flag){
					alert('订单新增成功');
				}else{
					alert('订单新增失败');
				}
			});
		}
	}
}();